package cn.iocoder.yudao.module.project.service.deliveryreceiptdetail;

import cn.iocoder.yudao.module.project.dal.mysql.internalcode.InternalCodeMapper;
import cn.iocoder.yudao.module.project.dal.mysql.orderdetail.OrderDetailMapper;
import com.alibaba.excel.util.DateUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.yudao.module.project.controller.admin.deliveryreceiptdetail.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.deliveryreceiptdetail.DeliveryReceiptDetailDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.project.dal.mysql.deliveryreceiptdetail.DeliveryReceiptDetailMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.project.enums.ErrorCodeConstants.*;

/**
 * 拨交单明细 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class DeliveryReceiptDetailServiceImpl implements DeliveryReceiptDetailService {

    @Resource
    private DeliveryReceiptDetailMapper deliveryReceiptDetailMapper;

    @Resource
    private OrderDetailMapper orderDetailMapper;

    @Resource
    private InternalCodeMapper internalCodeMapper;

    @Override
    public Integer createDeliveryReceiptDetail(DeliveryReceiptDetailSaveReqVO createReqVO) {
        // 插入
        DeliveryReceiptDetailDO deliveryReceiptDetail = BeanUtils.toBean(createReqVO, DeliveryReceiptDetailDO.class);
        deliveryReceiptDetailMapper.insert(deliveryReceiptDetail);
        // 返回
        return deliveryReceiptDetail.getId();
    }

    @Override
    public void updateDeliveryReceiptDetail(DeliveryReceiptDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateDeliveryReceiptDetailExists(updateReqVO.getId());
        // 更新
        DeliveryReceiptDetailDO updateObj = BeanUtils.toBean(updateReqVO, DeliveryReceiptDetailDO.class);
        deliveryReceiptDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeliveryReceiptDetail(Integer id) {
        // 校验存在
        validateDeliveryReceiptDetailExists(id);
        // 删除
        deliveryReceiptDetailMapper.deleteById(id);
    }

    private void validateDeliveryReceiptDetailExists(Integer id) {
        if (deliveryReceiptDetailMapper.selectById(id) == null) {
            throw exception(DELIVERY_RECEIPT_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public DeliveryReceiptDetailDO getDeliveryReceiptDetail(Integer id) {
        return deliveryReceiptDetailMapper.selectById(id);
    }

    @Override
    public PageResult<DeliveryReceiptDetailDO> getDeliveryReceiptDetailPage(DeliveryReceiptDetailPageReqVO pageReqVO) {
        Page<DeliveryReceiptDetailPageReqVO> page = new Page(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        List<DeliveryReceiptDetailDO> list = deliveryReceiptDetailMapper.selectPageList(page, pageReqVO);

        list.forEach(item -> {
            item.setSubmitTimeStr(DateUtils.format(item.getSubmitTime(), DateUtils.DATE_FORMAT_19));
            item.setLastCollectionTimeStr(DateUtils.format(item.getLastCollectionTime(), DateUtils.DATE_FORMAT_19));
        });

        PageResult<DeliveryReceiptDetailDO> pageResult = new PageResult<>(list, page.getTotal());
        return pageResult;
    }

    @Override
    public PageResult<DeliveryReceiptDetailDO> getReport(DeliveryReceiptDetailPageReqVO pageReqVO) {
        Page<DeliveryReceiptDetailPageReqVO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        List<DeliveryReceiptDetailDO> list = deliveryReceiptDetailMapper.getReport(page, pageReqVO);

        list.forEach(item -> {
            item.setSubmitTimeStr(DateUtils.format(item.getSubmitTime(), DateUtils.DATE_FORMAT_19));
            item.setLastCollectionTimeStr(DateUtils.format(item.getLastCollectionTime(), DateUtils.DATE_FORMAT_19));
        });

        PageResult<DeliveryReceiptDetailDO> pageResult = new PageResult<>(list, page.getTotal());
        return pageResult;
    }


    @Override
    public PageResult<DeliveryReceiptSummaryRespVO> getDeliveryReceiptSummaryReport(DeliveryReceiptDetailPageReqVO pageReqVO){
        Page<DeliveryReceiptDetailPageReqVO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        List<DeliveryReceiptSummaryRespVO> list = deliveryReceiptDetailMapper.getDeliveryReceiptSummaryReport(page, pageReqVO);

        PageResult<DeliveryReceiptSummaryRespVO> pageResult = new PageResult<>(list, page.getTotal());
        return pageResult;
    }

}